/*
 * EK.console
 * Inspired by Firebug
 * 
 * Enrichment Kit Console @console.version@ (http://enrichmentkit.com)
 * Copyright (c) 2008 Pink Hominid
 * Licensed under terms of the MIT License (LICENSE.txt)
 * 
 * IE 6.0+, FF 1.5+ Tested
 */
var EK = EK || {};
EK.console = new function EkConsole() {
    var that = this;
    var commandInput = undefined;
    var displayArea = undefined;
    var containerDiv = undefined;
    var cmdHistory = [];
    var cmdHistoryIdx = -1;
    
    this.browser = {
        version: (navigator.userAgent.match(
            /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/i) || [])[1],
        msie: /msie/i.test(navigator.userAgent) &&
            !(/opera/i.test(navigator.userAgent)),
        mozilla: /mozilla/i.test(navigator.userAgent) &&
            !(/(compatible|webkit)/i.test(navigator.userAgent))
    };
    this.toggle = function() {
        if (containerDiv) {
            var display = containerDiv.style.display;
            if (display === "none") {
                containerDiv.style.display = "block";
            } else {
                containerDiv.style.display = "none";
            }
        } else {
            containerDiv = document.getElementsByTagName('body')[0].appendChild(document.createElement('div'));
            containerDiv.id = "ekConsole";
            var position = "fixed";
            if (that.browser.msie && that.browser.version < 7) { position = "absolute"; }
            containerDiv.innerHTML = 
                '<form action="" style="opacity:.7;filter: alpha(opacity=70);width:50%;position:' + position + ';bottom:3px;right:3px;z-index:9999;" method="POST" onsubmit="return EK.console.evaluate();"><textarea id="ekDisplay" style="background-color:black;color:green;font-weight:bold;font-size:12px;border:1px solid #c9c7cb;width:100%;" rows="5" readonly="readonly"></textarea><br/><input id="ekCommand" style="width:100%;color:black;font-weight:bold;font-size:12px;" type="text" /></form>';
            commandInput = document.getElementById('ekCommand');
            displayArea = document.getElementById('ekDisplay');

            commandInput.onkeyup = function (e) {
                e = e || window.event;
                if (e.keyCode == 38) { // up arrow
                    if (cmdHistoryIdx > 0) { 
                        cmdHistoryIdx--;
                        commandInput.value = cmdHistory[cmdHistoryIdx];
                    }
                }
                else if (e.keyCode == 40) { // down arrow
                    if (cmdHistoryIdx < cmdHistory.length) { 
                        cmdHistoryIdx++;
                        commandInput.value = cmdHistory[cmdHistoryIdx] || "";
                    }
                }
                else if (e.keyCode == 27) { // escape key
                    commandInput.value = "";
                }
            };

        }
    };
    this.write = function (obj) {
        displayArea.value = displayArea.value + " >> " + obj + "\n";
        displayArea.scrollTop = displayArea.scrollHeight;
    };
    this.evaluate = function (cmd) {
        cmd = cmd || commandInput.value;
        try {
            if (typeof cmd == "string" && cmd.length) {
                cmdHistory.push(cmd);
                cmdHistoryIdx = cmdHistory.length;
                var result = eval(cmd);
                if (result === undefined) { that.write(cmd); }
                else if (result.toJSONString) { that.write(result.toJSONString()); }
                else { that.write(result); }
            }
        } catch(e) {
            that.write(e.message);
        } finally {
            commandInput.value = "";
            return false;
        }
    }; 
}();
// global console passive overrides
var console = console || {};
if (typeof(console.debug) !== "undefined") {
    EK.console.__debug = console.debug;
    console.debug = function () {
        EK.console.__debug.apply(this, arguments);
        EK.console.write.apply(this, arguments);
    }
} else { console.debug = EK.console.write; }
if (typeof(console.warn) !== "undefined") {
    EK.console.__warn = console.warn;
    console.warn = function () {
        EK.console.__warn.apply(this, arguments);
        EK.console.write.apply(this, arguments);
    }
} else { console.warn = EK.console.write; }
if (typeof(console.error) !== "undefined") {
    EK.console.__error = console.error;
    console.error = function () {
        EK.console.__error.apply(this, arguments);
        EK.console.write.apply(this, arguments);
    }
} else { console.error = EK.console.write; }
// start console
EK.console.toggle();